

var DY_MenuManager = function(target, data){
    var self = this;
    this.listTable = target.find('table.green-table');
    this.menus = {};
    this.subtotalEl = target.find('div.count').eq(0);
    this.feeEl = target.find('div.count').eq(1);
    this.totalEl = target.find('div.count').eq(2);
    this.count = {
        s: 0,
        f: 10,
    };
    
    this.updateCount = function(addCount){
        this.count.s = 0;
        for(var m in self.menus){
            self.count.s += self.menus[m].cost;
        }
        
        if(self.count.s > 1000){
            self.count.f = 100;
        }
        
        self.subtotalEl.children('span').text(self.count.s.toFixed(2) + ' 元');
        self.feeEl.children('span').text(self.count.f.toFixed(2) + ' 元');
        self.totalEl.children('span').text((self.count.s + self.count.f).toFixed(2) + ' 元');
    };
    this.addRow = function(id, name, price, num){
        if(!self.menus[id]){
            num = num || 1;
            var el = $('<tr><td>' + name + '</td><td class="num"><input type="text" value="' + 1 + '"/></td><td class="cost"><span>' + (price * num) + '</span></td><td class="cancel"><a href="#">Cancel</a></td></tr>');
            var costEl = el.find('td.cost span');
            var numEl = el.find('td.num input').keyup(function(e){
                var val = numEl.val();
                //validation
                if(!DY.isNum(val)){
                    val = 1;
                    numEl.val(1);
                }
                if(val.length > 2){
                    val = 99;
                    numEl.val(99);
                }
                var cost = numEl.val() * price
                costEl.html(cost);
                self.menus[id].cost = cost;
                self.updateCount();
            });
            var cancelEl = el.find('td.cancel a').click(function(e){
                self.removeRow(id);
                return false;
            });
            self.menus[id] = {
                el: el,
                name: name,
                price: price,
                cost: price * num,
                num: 1
            };
            self.listTable.append(el);
            self.updateCount();
        }else{
            self.menus[id].el.animate({opacity:0}, 300).animate({opacity:1}, 600);
        }
        
    };
    this.removeRow = function(id){
        self.menus[id].el.animate({opacity:0}, {
            speed: 500,
            complete: function(){
                self.menus[id].el.remove();
                self.menus[id] = null;
                delete self.menus[id];
            }
        });
        
    };
    
    //init
    for( var i = 0; i < data.length; i++ ){
        this.addRow(data[i].id, data[i].name, data[i].price);
    }
};

var DY_DetailViewer = function(menuListDom, viewerDom){
    var self = this;
    this.menuList = menuListDom;
    this.viewer = viewerDom;
    this.viewer.find('.hd a').click(function(){
        self.back();
        return false;
    });
    
    this.show = function(id){
        this.menuList.hide();
        this.viewer.show();
    };
    
    this.back = function(){
        this.menuList.show();
        this.viewer.hide();
    };
}

$(window).ready(function(){
    $(window).scroll(function() {
        if($(window).scrollTop() > 340){
            //$('#side-nav').addClass('side-nav_onfloat');
        }else{
            //$('#side-nav').removeClass('side-nav_onfloat');
        }
    });
    
    $('.slideshow').dySlideshow();
    $('.dy-tab').dyTabmenu();
    $('.dy-menu').dyMenu();
    
    var selectedMenu = [{
        id: 1,
        name: '3~6人份麻辣锅底',
        price: 120
    },{
        id: 2,
        name: '3人份麻辣锅底',
        price: 80
    },{
        id: 3,
        name: '8人份麻辣锅底',
        price: 150
    }];
    var menuManager = new DY_MenuManager($('#side-order'), selectedMenu);
    var detailViewer = new DY_DetailViewer($('#idx_menu'), $('#idx_detail'));
    
    //menu list
    $('#idx_menu').find('.menu-item').hover(function(e){
        $(this).addClass('menu-item-over');
    },function(e){
        $(this).removeClass('menu-item-over');
    }).click(function(e){
        var target = $(e.target);
        if(target.parent().hasClass('order')){
            menuManager.addRow($(this).data('mid'), $(this).find('h4 a').text(), $(this).data('price'), 1);
        }
        if(target.parent().parent().hasClass('pic')){
            detailViewer.show($(this).data('mid'));
        }
        return false;
    });
    
    //member panel
    $('#side-member').find('a.lnk-login').click(function(e){
        $('#side-member').find('.login').fadeIn(1000);
        $('#side-member').find('.haslogin').fadeOut(1000);
        $('#side-member').find('.nologin').fadeOut(1000);
        DY.hash('login');
        return false;
    });
    $('#side-member').find('a.lnk-reg').click(function(e){
        DY.openIframeWindow('register.html', 600, 400);
        return false;
    });
    $('#side-member').find('a.btn-login').click(function(e){
        //$('#side-member').find('.haslogin').fadeIn(1000);
        //$('#side-member').find('.login').fadeOut(1000);
        //$('#side-member').find('.nologin').fadeOut(1000);
        
        
        
        $('#side-member').find('.login').animate({top:-120}, function(){
            $('#side-member').find('.haslogin').show().animate({top:0});
        });
        $('#side-member').find('.nologin').hide();
        DY.hash('haslogin');
        return false;
    });
    $('#side-member').find('a.lnk-order').click(function(e){
        if(DY.hash() == '#haslogin'){
            $(this).parent().fadeOut(1000);
            $('#side-order').fadeIn(1000);
            $('#side-share, #side-help').fadeOut(1000);
        }else{
            $('#side-member').find('.login').fadeIn(1000);
            $('#side-member').find('.haslogin').fadeOut(1000);
            $('#side-member').find('.nologin').fadeOut(1000);
            DY.hash('login');
        }
        return false;
    })
    
    $('#btn-order-s1').click(function(e){
        $('#side-order .dy-tab .dy-tab-menu li').eq(0).removeClass('on');
        $('#side-order .dy-tab .dy-tab-menu li').eq(1).addClass('on');
        $('#side-order .dy-tab .dy-tab-panel').eq(0).hide();
        $('#side-order .dy-tab .dy-tab-panel').eq(1).show();
        return false;
    });
    
    
    //init UI
    var status = DY.hash();
    switch(status){
        case '#login':
            $('#side-member .login').show();
        break;
        case '#haslogin':
            $('#side-member .haslogin').show();
        break;
        case '#nologin':
        default:
            $('#side-member .nologin').show();
        break;
    }
    
 }); 